準備

演習で使用するパッケージを読み込みます.

if(!require(ggplot2)){ # 拡張パッケージはインストールされているか?
  # 拡張パッケージのインストール
  install.packages("ggplot2")
  # 拡張パッケージの読み込み
  library(ggplot2)
}
# PDFファイル用パッケージ
if(!require(pdftools)){ # 拡張パッケージはインストールされているか?
  # 拡張パッケージのインストール
  install.packages("pdftools")
  # 拡張パッケージの読み込み
  library(pdftools)
}

ggplot2で日本語を表示できるようにフォントを設定します.

if(.Platform$OS.type=="windows")
  windowsFonts(yugo=windowsFont("Yu Gothic"))
if(capabilities("aqua"))
  quartzFonts(yugo=quartzFont(rep("YuGo-Medium",4)))

★オープンデータの可視化

一つ星のオープンデータはCreative Commons等のOpen Licenseのもとで公開されたオープンデータです.

5 Star OPEN DATA

5 Star OPEN DATA

だれもが自由に利用できる状態にはありますが,利用者の利便性は考慮されていません.

それでは,実際に一つ星オープンデータを処理することで,その困難さを体験してみましょう.

事例として一橋大学の情報公開サイトで公開されている一橋大学データ集を利用します.

http://www.hit-u.ac.jp/guide/information/data/index.html

このサイトにある「外部資金」の「科学研究費助成事業(科学研究費補助金・学術研究助成基金助成金)」にあるPDF(Portable Document Format)ファイルを処理します.

PDFファイルには下記のように表データが記録されていますが,これをデータ処理するには一工夫しなければなりません.

PDFファイルの例

PDFファイルの例

本稿ではPDFファイルからテキストデータを抽出し,加工処理する演習を行います.

データ読込

まずは pdftoolsパッケージをつかってテキストデータを抽出します.

url = "http://www.hit-u.ac.jp/guide/information/data/pdf/06_kakenhi_2016.pdf"
#url = "day03/06_kakenhi_2016.pdf"
# PDFファイルからテキストを取り出します
res <- pdf_text(url)
# 変数の中身を確認します
res
## [1] "                                         2016年(平成28年)\n科学研究費助成事業(学術研究助成基金助成金/科学研究費補助金)\n                             交付額(円)\n     研究種目    交付件数\n                     直接経費                 間接経費\n新学術領域研究         0                0                      0\n基盤研究(S)         3       98,100,000             29,430,000\n基盤研究(A)        22      145,000,000             43,500,000\n基盤研究(B)        38      133,400,000             40,020,000\n基盤研究(C)        97      101,371,167             30,303,357\n挑戦的萌芽研究         5        4,100,000              1,230,000\n若手研究(A)         2        7,700,000              2,310,000\n若手研究(B)        23       23,798,457              7,139,538\n研究活動スタート支援      7        7,300,000              2,190,000\n研究成果公開促進費       1        1,099,872                      0\n特別研究員奨励費       67       53,600,000              3,480,000\n国際共同研究強化        4       34,300,000             10,290,000\n                       609,769,496            169,892,895\n   2015年度合計   269\n                             779,662,391\n"

PDFファイルから取り出したテキストが表示されます.ところどころに「」または「」という記号が含まれていますが,それらは改行を意味する制御記号です.

ファイルに出力してテキストエディタ等で開いてみましょう.

write(res,"tmp.txt")

余白からなんとなくもともとが表形式であったことがわかります.

データ加工

つぎに先程PDFから取り出したテキストを R で処理しやすくするために加工します.

ここでは CSV 形式への変換を試みます. CSV 形式とは,値をカンマ「,」で区切り,1行で1レコードを表現する表形式のデータフォーマットです.

たとえば,以下の表をCSV形式に変換すると,

ID 氏名 血液型
0001 山田太郎 A
0002 山田花子 B
0003 山田次郎 O

こうなります.

ID,氏名,血液型
,,
0001,山田太郎,A
0002,山田花子,B
0003,山田次郎,O

カンマ「,」以外にタブ「」を区切り文字として利用する場合は TSV(Tab Separated Value) 形式と呼ばれます.

それでは早速先程のテキストを CSV 形式にしてみましょう.

ここでは文字列置換による手法をとります.

まず,CSV形式の妨げとなる3桁区切りのカンマを除去します.

res<-gsub(",","",res)

gsub 関数は文字列置換のための関数で以下のように使います.

置換後のオブジェクト<- gsub(置換文字のパターン,置換後の文字,置換前のオブジェクト)

つぎに複数の空白文字を一つのカンマに置き換えます.

res<-gsub(" +",",",res)

ここで置換文字のパターンとして「 +」が指定されていますが, これは正規表現と呼ばれ,ここでの意味は「複数の連続する空白文字」を表すパターンとなります.

加工後のオブジェクトをファイルに書き出してみましょう.

write(res,"kaken16.txt")

書き出したファイルをテキストエディタで開いてみると, 行毎に列数は異なるものの CSV 形式として出力されていることがわかります.

CSVファイルの読み込み

つぎに書き出した CSV ファイルを read.csv 関数で読み込みます. (オブジェクトに格納されたテキストから直接読み込むこともできますが,Windowsで発生した不具合対応のため,一度ファイルに出力しています.)

基本的に read.csv 関数は表形式のCSVファイルをデータフレーム形式に変換する関数です. 今回のように列数が異なるCSV形式を読み込むことはできません. 直接ファイルを修正してもよいのですが,今回は引数skipをread.csv関数に渡すことでエラーとなる行をスキップします.

#df16<-read.csv(text=res,skip = 5,header = FALSE,stringsAsFactors = FALSE)
# CSVファイルを読み込む.条件は,5行スキップ,ヘッダー・因子(Factor)への自動変換なし
df16<-read.csv("kaken16.txt",skip = 5,header = FALSE,stringsAsFactors = FALSE)
#表示する
df16
##                      V1           V2        V3       V4
## 1        新学術領域研究            0         0        0
## 2         基盤研究(S)            3  98100000 29430000
## 3         基盤研究(A)           22 145000000 43500000
## 4         基盤研究(B)           38 133400000 40020000
## 5         基盤研究(C)           97 101371167 30303357
## 6        挑戦的萌芽研究            5   4100000  1230000
## 7         若手研究(A)            2   7700000  2310000
## 8         若手研究(B)           23  23798457  7139538
## 9  研究活動スタート支援            7   7300000  2190000
## 10   研究成果公開促進費            1   1099872        0
## 11     特別研究員奨励費           67  53600000  3480000
## 12     国際共同研究強化            4  34300000 10290000
## 13                         609769496 169892895       NA
## 14                      2015年度合計       269       NA
## 15                         779662391        NA       NA

read.csv 関数はデータフレームを返します.

列名は自動的に割り当てられているので注意して下さい. 列名は以下で確認できます.

colnames(df16)
## [1] "V1" "V2" "V3" "V4"

今回の例では試せませんが, read.csv の実行時 header = TRUE とすると1行目をヘッダー(列名)として利用することもできます.

データ整形

もう一度,データフレームの内容を確認してみましょう.

df16
##                      V1           V2        V3       V4
## 1        新学術領域研究            0         0        0
## 2         基盤研究(S)            3  98100000 29430000
## 3         基盤研究(A)           22 145000000 43500000
## 4         基盤研究(B)           38 133400000 40020000
## 5         基盤研究(C)           97 101371167 30303357
## 6        挑戦的萌芽研究            5   4100000  1230000
## 7         若手研究(A)            2   7700000  2310000
## 8         若手研究(B)           23  23798457  7139538
## 9  研究活動スタート支援            7   7300000  2190000
## 10   研究成果公開促進費            1   1099872        0
## 11     特別研究員奨励費           67  53600000  3480000
## 12     国際共同研究強化            4  34300000 10290000
## 13                         609769496 169892895       NA
## 14                      2015年度合計       269       NA
## 15                         779662391        NA       NA

つぎは列ごとに内容を確認してみましょう.

df16$V1
##  [1] "新学術領域研究"       "基盤研究(S)"        "基盤研究(A)"       
##  [4] "基盤研究(B)"        "基盤研究(C)"        "挑戦的萌芽研究"      
##  [7] "若手研究(A)"        "若手研究(B)"        "研究活動スタート支援"
## [10] "研究成果公開促進費"   "特別研究員奨励費"     "国際共同研究強化"    
## [13] ""                     ""                     ""
df16$V2
##  [1] "0"            "3"            "22"           "38"          
##  [5] "97"           "5"            "2"            "23"          
##  [9] "7"            "1"            "67"           "4"           
## [13] "609769496"    "2015年度合計" "779662391"
df16$V3
##  [1]         0  98100000 145000000 133400000 101371167   4100000   7700000
##  [8]  23798457   7300000   1099872  53600000  34300000 169892895       269
## [15]        NA
df16$V4
##  [1]        0 29430000 43500000 40020000 30303357  1230000  2310000
##  [8]  7139538  2190000        0  3480000 10290000       NA       NA
## [15]       NA

13行目以降のデータが少しおかしなことになっています.

該当箇所を抜き出してみましょう.

#行数を格納する
tmp<-nrow(df16)
#13行目から最終行までの連続値を作成する(ベクトル形式)
13:tmp
## [1] 13 14 15
#該当箇所を表示する
df16[13:tmp,]
##    V1           V2        V3 V4
## 13       609769496 169892895 NA
## 14    2015年度合計       269 NA
## 15       779662391        NA NA

合計金額等はあとから計算すればよいので13行目以降は削除することにします.

このようにデータフレームから不要な行を削除するには, 指定した行に NULL を代入するか,特定行を除いた形でのデータフレームを再構成します. ここでは特定行を除いた形でデータフレームを再構成します.

要するに該当箇所以外を選択します.次のコマンドを実行してみてください.

df16[-(13:nrow(df16)),]
##                      V1 V2        V3       V4
## 1        新学術領域研究  0         0        0
## 2         基盤研究(S)  3  98100000 29430000
## 3         基盤研究(A) 22 145000000 43500000
## 4         基盤研究(B) 38 133400000 40020000
## 5         基盤研究(C) 97 101371167 30303357
## 6        挑戦的萌芽研究  5   4100000  1230000
## 7         若手研究(A)  2   7700000  2310000
## 8         若手研究(B) 23  23798457  7139538
## 9  研究活動スタート支援  7   7300000  2190000
## 10   研究成果公開促進費  1   1099872        0
## 11     特別研究員奨励費 67  53600000  3480000
## 12     国際共同研究強化  4  34300000 10290000

該当箇所以外のデータが表示されたかと思います. このように行を指定する際,マイナス記号「-」を付けると指定した行以外を選択することができます.

それでは,13行目から最終行までを削除しましょう.

df16<-df16[-(13:nrow(df16)),]
df16
##                      V1 V2        V3       V4
## 1        新学術領域研究  0         0        0
## 2         基盤研究(S)  3  98100000 29430000
## 3         基盤研究(A) 22 145000000 43500000
## 4         基盤研究(B) 38 133400000 40020000
## 5         基盤研究(C) 97 101371167 30303357
## 6        挑戦的萌芽研究  5   4100000  1230000
## 7         若手研究(A)  2   7700000  2310000
## 8         若手研究(B) 23  23798457  7139538
## 9  研究活動スタート支援  7   7300000  2190000
## 10   研究成果公開促進費  1   1099872        0
## 11     特別研究員奨励費 67  53600000  3480000
## 12     国際共同研究強化  4  34300000 10290000

つぎに型変換を行います. 基本的に read.csv 関数が値の内容を見て,文字列と数値を区別してくれるのですが, ときどき判定に失敗することがあります.

as.numeric 関数を使って値の型を数値に変換します.

df16$V2<-as.numeric(df16$V2)
df16$V3<-as.numeric(df16$V3)
df16$V4<-as.numeric(df16$V4)

文字列に変換する場合は as.character 関数を用います.

なお,変換に失敗すると NA が戻ります.

as.numeric("words")
## [1] NA

つぎに欠損値(NA)対応を行います.ここではNAを0に置換します.

#NAであれば0を代入する
df16$V2[is.na(df16$V2)]<-0
df16$V3[is.na(df16$V3)]<-0
df16$V4[is.na(df16$V4)]<-0

最後に各列の名前をつけます.

colnames(df16)<-c("type","count","direct","indirect")
df16
##                    type count    direct indirect
## 1        新学術領域研究     0         0        0
## 2         基盤研究(S)     3  98100000 29430000
## 3         基盤研究(A)    22 145000000 43500000
## 4         基盤研究(B)    38 133400000 40020000
## 5         基盤研究(C)    97 101371167 30303357
## 6        挑戦的萌芽研究     5   4100000  1230000
## 7         若手研究(A)     2   7700000  2310000
## 8         若手研究(B)    23  23798457  7139538
## 9  研究活動スタート支援     7   7300000  2190000
## 10   研究成果公開促進費     1   1099872        0
## 11     特別研究員奨励費    67  53600000  3480000
## 12     国際共同研究強化     4  34300000 10290000

グラフ描画

研究種目別・交付件数をグラフにします.

g1<-ggplot(df16,aes(x=type,y=count))
g1

#棒グラフの描画
g1<-g1+geom_bar(stat="identity")
g1

#軸タイトル,グラフタイトルの設定
g1<-g1+xlab("研究種目")+ylab("交付件数")+ggtitle("2016年 科学研究費補助金")
g1

#日本語フォント設定
g1<-g1+theme_gray(base_family ="yugo")
g1

#軸ラベルを縦にする
g1<-g1+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g1

交付額もグラフにしてみます.

g2<-ggplot(df16,aes(x=type,y=(direct+indirect)/1000))+geom_bar(stat = "identity",colour = "magenta",fill="magenta",linetype = 2,size = 0.5 )
g2

g2<-g2+xlab("研究種目")+ylab("交付額(千円)")+ggtitle("2016年 科学研究費補助金")
g2

g2<-g2+theme_bw(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g2

g2<-g2+theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background = element_blank())
g2

geom_bar を geom_line に変更すれば折れ線グラフも描けます.

g2<-ggplot(df16,aes(x=type,y=(direct+indirect)/1000,group=1))+geom_line(colour = "magenta",linetype = 2,size = 0.5 )

ggplot2には様々なグラフ関数が含まれています.興味がある人はggplot2のドキュメントを覗いてみてください.

http://docs.ggplot2.org/current/

作業環境(ワークスペース)の保存&読み込み

作業を一旦中断する場合などは, 作業環境をファイルに保存しておくと便利です.

作業環境を保存する場合は以下を実行します.

save(list = ls(all.names = TRUE), file = "all.RData")

作業環境を読み込む場合は以下を実行します.

#作業環境をクリアする
rm(list=ls())
#作業環境を読み込む
load("all.RData")

作業環境を保存しておけば, 作業の続きから開始することができます.

★オープンデータの可視化(年別)

2015年

2016年と同じように2015年のデータも可視化してみよう.

url = "http://www.hit-u.ac.jp/guide/information/data/pdf/06_kakenhi_2015.pdf"
res <- pdf_text(url)
res<-gsub(",","",res[1])
res<-gsub(" +",",",res[1])
#ファイル名を変更する
write(res,"kaken15.txt")
#変数名も変更する
df15<-read.csv("kaken15.txt",skip = 5,header = FALSE,stringsAsFactors = FALSE)
df15<-df15[-(12:nrow(df15)),]



df15$V2<-as.numeric(df15$V2)
df15$V3<-as.numeric(df15$V3)
df15$V4<-as.numeric(df15$V4)

df15$V2[is.na(df15$V2)]<-0
df15$V3[is.na(df15$V3)]<-0
df15$V4[is.na(df15$V4)]<-0

colnames(df15)<-c("type","count","direct","indirect")
g1<-ggplot(df15,aes(x=type,y=count))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付件数")+ggtitle("2015年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g1

g2<-ggplot(df15,aes(x=type,y=(direct+indirect)/1000,group=1))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付額(千円)")+ggtitle("2015年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g2

2014年

url = "http://www.hit-u.ac.jp/guide/information/data/pdf/06_kakenhi_2014.pdf"
res <- pdf_text(url)
#res<-iconv(res[1],to="UTF-8")
res<-gsub(",","",res[1])
res<-gsub(" +",",",res[1])
write(res,"kaken14.txt")
#df14<-read.csv(text=res[1],skip = 5,header = FALSE,stringsAsFactors = FALSE)
df14<-read.csv(file="kaken14.txt",skip = 5,header = FALSE,stringsAsFactors = FALSE)
df14<-df14[-(13:nrow(df14)),]

df14$V2<-as.numeric(df14$V2)
df14$V3<-as.numeric(df14$V3)
df14$V4<-as.numeric(df14$V4)

df14$V2[is.na(df14$V2)]<-0
df14$V3[is.na(df14$V3)]<-0
df14$V4[is.na(df14$V4)]<-0

colnames(df14)<-c("type","count","direct","indirect")
g1<-ggplot(df14,aes(x=type,y=count))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付件数")+ggtitle("2014年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g1

g2<-ggplot(df14,aes(x=type,y=(direct+indirect)/1000,group=1))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付額(千円)")+ggtitle("2014年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g2

2013年

url = "http://www.hit-u.ac.jp/guide/information/data/pdf/06_kakenhi_2013.pdf"
res <- pdf_text(url)
res<-gsub(",","",res[1])
res<-gsub(" +",",",res[1])
write(res,"kaken13.txt")
#df13<-read.csv(text=res[1],skip = 5,header = FALSE,stringsAsFactors = FALSE)
df13<-read.csv(file = "kaken13.txt",skip = 5,header = FALSE,stringsAsFactors = FALSE)
df13<-df13[-(13:nrow(df13)),]

df13$V2<-as.numeric(df13$V2)
df13$V3<-as.numeric(df13$V3)
df13$V4<-as.numeric(df13$V4)

df13$V2[is.na(df13$V2)]<-0
df13$V3[is.na(df13$V3)]<-0
df13$V4[is.na(df13$V4)]<-0

colnames(df13)<-c("type","count","direct","indirect")
g1<-ggplot(df13,aes(x=type,y=count))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付件数")+ggtitle("2013年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g1

g2<-ggplot(df13,aes(x=type,y=(direct+indirect)/1000,group=1))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付額(千円)")+ggtitle("2013年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g2

2012年

url = "http://www.hit-u.ac.jp/guide/information/data/pdf/06_kakenhi_2012.pdf"
res <- pdf_text(url)
res<-gsub(",","",res[1])
res<-gsub(" +",",",res[1])
write(res,"kaken12.txt")
df12<-read.csv(file="kaken12.txt",skip = 5,header = FALSE,stringsAsFactors = FALSE)
#df12<-read.csv(text=res[1],skip = 5,header = FALSE,stringsAsFactors = FALSE)
df12<-df12[-(13:nrow(df12)),]

df12$V2<-as.numeric(df12$V2)
df12$V3<-as.numeric(df12$V3)
df12$V4<-as.numeric(df12$V4)

df12$V2[is.na(df12$V2)]<-0
df12$V3[is.na(df12$V3)]<-0
df12$V4[is.na(df12$V4)]<-0

colnames(df12)<-c("type","count","direct","indirect")
g1<-ggplot(df12,aes(x=type,y=count))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付件数")+ggtitle("2012年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g1

g2<-ggplot(df12,aes(x=type,y=(direct+indirect)/1000,group=1))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付額(千円)")+ggtitle("2012年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g2

2011年

url = "http://www.hit-u.ac.jp/guide/information/data/pdf/06_kakenhi_2011.pdf"
res <- pdf_text(url)
#res<-iconv(res[1],to="UTF-8")
res<-gsub(",","",res[1])
res<-gsub(" +",",",res[1])
write(res,"kaken11.txt")
#df11<-read.csv(text=res[1],skip = 5,header = FALSE,stringsAsFactors = FALSE)
df11<-read.csv(file="kaken11.txt",skip = 5,header = FALSE,stringsAsFactors = FALSE)
df11<-df11[-(15:nrow(df11)),]

df11$V2<-as.numeric(df11$V2)
df11$V3<-as.numeric(df11$V3)
df11$V4<-as.numeric(df11$V4)

df11$V2[is.na(df11$V2)]<-0
df11$V3[is.na(df11$V3)]<-0
df11$V4[is.na(df11$V4)]<-0

colnames(df11)<-c("type","count","direct","indirect")
g1<-ggplot(df11,aes(x=type,y=count))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付件数")+ggtitle("2011年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g1

g2<-ggplot(df11,aes(x=type,y=(direct+indirect)/1000,group=1))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付額(千円)")+ggtitle("2011年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g2

2010年

url = "http://www.hit-u.ac.jp/guide/information/data/pdf/06_kakenhi_2010.pdf"
res <- pdf_text(url)
res<-gsub(",","",res[1])
res<-gsub(" +",",",res[1])
write(res,"kaken10.txt")
#df10<-read.csv(text=res[1],skip = 5,header = FALSE,stringsAsFactors = FALSE)
df10<-read.csv(file = "kaken10.txt",skip = 5,header = FALSE,stringsAsFactors = FALSE)
df10<-df10[-(15:nrow(df10)),]

df10$V2<-as.numeric(df10$V2)
df10$V3<-as.numeric(df10$V3)
df10$V4<-as.numeric(df10$V4)

df10$V2[is.na(df10$V2)]<-0
df10$V3[is.na(df10$V3)]<-0
df10$V4[is.na(df10$V4)]<-0

colnames(df10)<-c("type","count","direct","indirect")
g1<-ggplot(df10,aes(x=type,y=count))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付件数")+ggtitle("2010年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g1

g2<-ggplot(df10,aes(x=type,y=(direct+indirect)/1000,group=1))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付額(千円)")+ggtitle("2010年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g2

2009年

この年度はちょっとだけデータ形式が異なるので注意が必要

url = "http://www.hit-u.ac.jp/guide/information/data/pdf/06_kakenhi_2009.pdf"
res <- pdf_text(url)
#res<-iconv(res[1],to="UTF-8")
res<-gsub(",","",res[1])
res<-gsub(" +",",",res[1])
write(res,"kaken09.txt")
#df09<-read.csv(text=res[1],skip = 6,header = FALSE,stringsAsFactors = FALSE)
df09<-read.csv(file = "kaken09.txt",skip = 6,header = FALSE,stringsAsFactors = FALSE)
df09<-df09[-(12:nrow(df09)),]

df09$V2<-as.numeric(df09$V2)
df09$V3<-as.numeric(df09$V3)
df09$V4<-as.numeric(df09$V4)

df09$V2[is.na(df09$V2)]<-0
df09$V3[is.na(df09$V3)]<-0
df09$V4[is.na(df09$V4)]<-0

colnames(df09)<-c("type","count","direct","indirect")
g1<-ggplot(df09,aes(x=type,y=count))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付件数")+ggtitle("2009年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g1

g2<-ggplot(df09,aes(x=type,y=(direct+indirect)/1000,group=1))+geom_bar(stat="identity")+xlab("研究種目")+ylab("交付額(千円)")+ggtitle("2009年 科学研究費補助金")+theme_gray(base_family ="yugo")+theme(axis.text.x = element_text(angle = 90, hjust = 1))
g2

★オープンデータの可視化(年次推移)

これまでは年ごとに科研費の交付額・件数を可視化してきたが, 年ごとの推移をみてみたいと思います.

kaken_c<-cbind(df16[df16$type=="基盤研究(C)",],year=2016)
kaken_c<-rbind(kaken_c,cbind(df15[df15$type=="基盤研究(C)",],year=2015))
kaken_c<-rbind(kaken_c,cbind(df14[df14$type=="基盤研究(C)",],year=2014))
kaken_c<-rbind(kaken_c,cbind(df13[df13$type=="基盤研究(C)",],year=2013))
kaken_c<-rbind(kaken_c,cbind(df12[df12$type=="基盤研究(C)",],year=2012))
kaken_c<-rbind(kaken_c,cbind(df11[df11$type=="基盤研究(C)",],year=2011))
kaken_c<-rbind(kaken_c,cbind(df10[df10$type=="基盤研究(C)",],year=2010))
kaken_c<-rbind(kaken_c,cbind(df09[df09$type=="基盤研究(C)",],year=2009))

colnames(kaken_c)<-c("type","count","direct","indirect","year")
kaken_c$type <- "基盤研究(C)"

g1<-ggplot(kaken_c,aes(x=year,y=count))+geom_bar(stat="identity")+xlab("年")+ylab("件数")+theme_bw(base_family ="yugo")+theme(plot.margin = grid::unit(c(.5, 1, .5, 0), "cm"))
g1

g2<-ggplot(kaken_c,aes(x=year,y=(direct+indirect)/1000))+geom_line(stat="identity")+ylab("経費(千円)")+theme_bw(base_family ="yugo")+theme(plot.margin = grid::unit(c(.5, 1, .5, 0), "cm"),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background = element_blank())
g2

kaken_b<-cbind(df16[df16$type=="基盤研究(B)",],year=2016)
kaken_b<-rbind(kaken_b,cbind(df15[df15$type=="基盤研究(B)",],year=2015))
kaken_b<-rbind(kaken_b,cbind(df14[df14$type=="基盤研究(B)",],year=2014))
kaken_b<-rbind(kaken_b,cbind(df13[df13$type=="基盤研究(B)",],year=2013))
kaken_b<-rbind(kaken_b,cbind(df12[df12$type=="基盤研究(B)",],year=2012))
kaken_b<-rbind(kaken_b,cbind(df11[df11$type=="基盤研究(B)",],year=2011))
kaken_b<-rbind(kaken_b,cbind(df10[df10$type=="基盤研究(B)",],year=2010))
kaken_b<-rbind(kaken_b,cbind(df09[df09$type=="基盤研究(B)",],year=2009))

colnames(kaken_b)<-c("type","count","direct","indirect","year")
kaken_b$type <- "基盤研究(B)"

g1<-ggplot(kaken_b,aes(x=year,y=count))+geom_bar(stat="identity")+xlab("年")+ylab("件数")+theme_bw(base_family ="yugo")+theme(plot.margin = grid::unit(c(.5, 1, .5, 0), "cm"))
g1

g2<-ggplot(kaken_b,aes(x=year,y=((direct+indirect)/1000)))+geom_line(stat="identity")+ylab("経費(千円)")+theme_bw(base_family ="yugo")+theme(plot.margin = grid::unit(c(.5, 1, .5, 0), "cm"),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background = element_blank())
g2

kaken_a<-cbind(df16[df16$type=="基盤研究(A)",],year=2016)
kaken_a<-rbind(kaken_a,cbind(df15[df15$type=="基盤研究(A)",],year=2015))
kaken_a<-rbind(kaken_a,cbind(df14[df14$type=="基盤研究(A)",],year=2014))
kaken_a<-rbind(kaken_a,cbind(df13[df13$type=="基盤研究(A)",],year=2013))
kaken_a<-rbind(kaken_a,cbind(df12[df12$type=="基盤研究(A)",],year=2012))
kaken_a<-rbind(kaken_a,cbind(df11[df11$type=="基盤研究(A)",],year=2011))
kaken_a<-rbind(kaken_a,cbind(df10[df10$type=="基盤研究(A)",],year=2010))
kaken_a<-rbind(kaken_a,cbind(df09[df09$type=="基盤研究(A)",],year=2009))

colnames(kaken_a)<-c("type","count","direct","indirect","year")
kaken_a$type <- "基盤研究(A)"

g1<-ggplot(kaken_a,aes(x=year,y=count))+geom_bar(stat="identity")+xlab("年")+ylab("件数")+theme_bw(base_family ="yugo")+theme(plot.margin = grid::unit(c(.5, 1, .5, 0), "cm"))
g1

g2<-ggplot(kaken_a,aes(x=year,y=((direct+indirect)/1000)))+geom_line(stat="identity")+ylab("経費(千円)")+theme_bw(base_family ="yugo")+theme(plot.margin = grid::unit(c(.5, 1, .5, 0), "cm"),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background = element_blank())
g2

#ggdual_axis(lhs = g1, rhs = g2)
kaken_s<-cbind(df16[df16$type=="基盤研究(S)",],year=2016)
kaken_s<-rbind(kaken_s,cbind(df15[df15$type=="基盤研究(S)",],year=2015))
kaken_s<-rbind(kaken_s,cbind(df14[df14$type=="基盤研究(S)",],year=2014))
kaken_s<-rbind(kaken_s,cbind(df13[df13$type=="基盤研究(S)",],year=2013))
kaken_s<-rbind(kaken_s,cbind(df12[df12$type=="基盤研究(S)",],year=2012))
kaken_s<-rbind(kaken_s,cbind(df11[df11$type=="基盤研究(S)",],year=2011))
kaken_s<-rbind(kaken_s,cbind(df10[df10$type=="基盤研究(S)",],year=2010))
kaken_s<-rbind(kaken_s,cbind(df09[df09$type=="基盤研究(S)",],year=2009))

colnames(kaken_s)<-c("type","count","direct","indirect","year")
kaken_s$type <- "基盤研究(S)"

g1<-ggplot(kaken_s,aes(x=year,y=count))+geom_bar(stat="identity")+xlab("年")+ylab("件数")+theme_bw(base_family ="yugo")+theme(plot.margin = grid::unit(c(.5, 1, .5, 0), "cm"))
g1

g2<-ggplot(kaken_s,aes(x=year,y=((direct+indirect)/1000)))+geom_line(stat="identity")+ylab("経費(千円)")+theme_bw(base_family ="yugo")+theme(plot.margin = grid::unit(c(.5, 1, .5, 0), "cm"),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background = element_blank())
g2

#ggdual_axis(lhs = g1, rhs = g2)
kaken <- rbind(kaken_c,kaken_b,kaken_a,kaken_s)
ggplot(kaken,aes(x=year,y=count,fill=type))+geom_bar(stat="identity")+ggtitle("科研費基盤研究:交付件数の推移")+theme_bw(base_family ="yugo")

ggplot(kaken,aes(x=year,y=(direct+indirect)/1000,fill=type))+geom_bar(stat="identity")+ylab("経費(千円)")+ggtitle("科研費基盤研究:交付経費の推移")+theme_bw(base_family ="yugo")+scale_fill_manual(values = c("red","blue","yellow","green"))

res<-pdf_text("http://www.hit-u.ac.jp/guide/information/data/pdf/04-1_library_holdings.pdf")
res<-gsub(",","",res[1])
res<-gsub(" +",",",res[1])
tbl<-read.csv(text=res[1],header = FALSE,stringsAsFactors = FALSE)
df<-data.frame(t(tbl[3,3:10]),t(tbl[15,3:10]))
colnames(df)<-c("date","onlinejournal")
df$onlinejournal<-as.numeric(as.character(df$onlinejournal))
df$year<-c(2015,2014,2013,2012,2011,2010,2009,2008)
ggplot(df,aes(x=year,y=onlinejournal))+geom_bar(stat="identity")+geom_smooth(method = "lm")

save(list = ls(all.names = TRUE), file = "day03/all.RData")